package com.starinside.wp.repository;

import com.starinside.wp.domain.WxUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface WxUserRepository extends JpaRepository<WxUser,String> , JpaSpecificationExecutor<WxUser> {

    Optional<WxUser> findFirstByParentMobileAndSourceAppIdIsNullAndOpenidIsNullAndOpenidWpIsNull(String parentMobile);


    Optional<WxUser> findById(String id);

    Optional<WxUser> findByOpenidWp(String s);
    Optional<WxUser> findByUnionid(String s);

    @Query(value = "select u.* from wx_user u where u.parent_mobile like concat('%', ?1)", nativeQuery = true)
    List<WxUser> findByParentMobileLike(String mobileTail);

    @Query(value = "select u.* from wx_user u where u.parent_id in ?1", nativeQuery = true)
    List<WxUser> findByParentIdIn(String[] parentIds);

    List<WxUser> findAllByParentIdInAndSourceAppId(String[] parentIds, String appId);

    @Query(value = "select u.* from wx_user u where u.parent_id in ?1 and (u.source_appid = ?2 or u.source_appid is null)", nativeQuery = true)
    List<WxUser> findAllByParentIdInAndSourceAppIdOptional(String[] parentIds, String appId);

    Optional<WxUser> findByParentMobileAndSourceAppId(String parentMobile,String sourceAppId);
}
